<template>
  <div class="index">
    <van-tabs v-model="active">
      <van-tab
        v-for="channel in channels"
        :title="channel.name"
        :key="channel.id"
      >
        <ArticleList :channel="channel" />
      </van-tab>
    </van-tabs>

    <!-- 添加文章管理按钮 -->
    <div class="bar-btn" @click="isShowChannelEdit = true">
      <van-icon name="wap-nav" />
    </div>
    <!-- 弹层 -->
    <van-action-sheet v-model="isShowChannelEdit" title="标题">
      <ChannelEdit
        :curIndex="active"
        @change-channel="hChangeChannel"
        @update-active="hUpdateActive"
        :channels="channels"
      ></ChannelEdit>
    </van-action-sheet>
  </div>
</template>

<script>
import ArticleList from './articleList'
import ChannelEdit from '../hone/channelEdit.vue'
import { getChannels } from '../../api/channel.js'
export default {

  name: 'home',
  components: {
    ArticleList,
    ChannelEdit
  },
  data () {
    return {
      active: 2,
      channels: [],
      isShowChannelEdit: false
    }
  },

  created () {
    this.loadChannels()
  },
  methods: {
    // 收到了子组件中的修正active事件
    hUpdateActive () {
      this.active--
    },
    hChangeChannel (index) {
      // 关弹窗
      this.isShowChannelEdit = false
      this.active = index
    },
    async loadChannels () {
      try {
        const res = await getChannels()
        this.channels = res.data.data.channels
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style lang="less" scoped>
// 频道管理的开关按钮
.bar-btn {
  position: fixed;
  right: 5px;
  top: 57px;
  display: flex;
  align-items: center;
  background-color: #fff;
  opacity: 0.8;
  z-index: 1;
  .van-icon-wap-nav {
    font-size: 20px;
  }
}
</style>
